package com.zhangyj.action;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import com.zhangyj.entitis.SystemParam;

public class RequestContext extends HttpServletRequestWrapper {
	private Hashtable<String, String> params;
	private String action = null;

	public RequestContext(HttpServletRequest request) {
		super(request);
		 SystemParam.getSystemParam(request);
		params = new Hashtable<String, String>();
		
		String contextPath = request.getContextPath();
		String requestUri = this.extractRequestUri(request.getRequestURI(), contextPath);
		// 3是. do的长度
		requestUri = requestUri.substring(0, requestUri.length() - 3);
		String[] urlModel = requestUri.split("/");
		int baseLength = 2;
		int actionIndex = 1;
		UrlPattern url = null;
		if (urlModel.length > baseLength) {
			String key = urlModel[1] + "." + (urlModel.length - baseLength);
			url = BoastContext.getUrlPattern(key);
		}
		if (url != null) {
			if (url.getSize() == urlModel.length - baseLength) {
				for (int i = 0; i < url.getSize(); i++) {
					this.addParameter(url.getVars()[i], urlModel[i + baseLength]);
				}
			}
		}
		this.action = urlModel[actionIndex];

	}

	
	public String getParameter(String name) {
		String val = null;
		if (params != null) {
			val = params.get(name);
		}
		if (val != null) {
			return val;
		}
		// TODO Auto-generated method stub
		return super.getParameter(name);
	}

	/**
	 * 去除Uri中的jsessionid
	 * 
	 * @param requestUri
	 * @param contextPath
	 * @return
	 */
	private String extractRequestUri(String requestUri, String contextPath) {
		if (contextPath != null && contextPath.length() > 0) {
			requestUri = requestUri.substring(contextPath.length(), requestUri.length());
		}
		int index = requestUri.indexOf(';');
		if (index > -1) {
			int lastIndex = requestUri.indexOf('?');
			if (lastIndex == -1) {
				lastIndex = requestUri.indexOf('&');
			}

			if (lastIndex == -1) {
				requestUri = requestUri.substring(0, index);
			} else {
				String part1 = requestUri.substring(0, index);
				requestUri = part1 + requestUri.substring(lastIndex);
			}
		}
		return requestUri;
	}

	private void addParameter(String key, String value) {
		this.params.put(key, value);
	}

	public String getAction() {
		return this.action;
	}


	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Map getParameterMap() {
		Map param = new HashMap<String, String>(super.getParameterMap());
		param.putAll(params);
		return param;
	}


	@SuppressWarnings("rawtypes")
	@Override
	public Enumeration getParameterNames() {
		Enumeration enumeration =  super.getParameterNames();
		 Vector<Object> v = new Vector<Object>();  
		while(enumeration.hasMoreElements()) {
			v.add(enumeration.nextElement());
		}
		v.addAll(params.keySet());
		return v.elements();
	}
	
}
